import axios from 'axios'
import { ElMessage } from 'element-plus'

// 创建axios实例
const service = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  // baseURL: 'http://192.168.10.19',
  baseURL: 'http://123.249.83.87:6705',
  // 超时
  timeout: 10000
})

// request拦截器
service.interceptors.request.use(config => {
  return config
}, error => {
  console.log(error)
  Promise.reject(error)
})

// 响应拦截器
service.interceptors.response.use(response => {
  const { code, data, msg } = response.data
  if (code == 200) {
    return data
  } else {
    ElMessage.error(msg)
    return Promise.reject(new Error(msg))
  }
},
  error => {
    ElMessage.error(error.message)
    return Promise.reject(error)
  }
)

export default service
